<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaDutyBaseController;

class OaRoleDutyController extends OaDutyBaseController
{
    protected $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_role_duty');
        parent::_initialize();

    }
    function lists(){

        // 解析列表规则
        $list_data = $this->_get_model_list($this->model);

        $show_data = array();

        $duty = db_select('oa_duty', null, 'id asc');
        foreach($duty as $i => $d) {
            $show_data[$i]['duty_id'] = $d['name'];
            $show_data[$i]['id'] = $d['id'];
            $role_duty = db_select('oa_role_duty', ['duty_id'=>$d['id']]);
            foreach($role_duty as $rd) {
                $role = db_find('oa_role', ['id'=>$rd['role_id']]);
                if($show_data[$i]['role_id'] == NULL)
                    $show_data[$i]['role_id'] = $role['name'];
                else
                    $show_data[$i]['role_id'] = $show_data[$i]['role_id'].'  /  '.$role['name'];
            }
            $editUrl = U('edit', array('id'=> $d['id']));
            $delUrl = U('del', array('id'=> $d['id']));
            $show_data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";
        }
        $this->assign( 'list_grids', $list_data['list_grids']);
        $this->assign('list_data', $show_data);
        $this->display();
    }

    function add(){
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['duty_id'] = $_POST['duty_id'];
            $role = db_select('oa_role');
            $oldDutyRoles = db_select('oa_role_duty', ['duty_id'=>$data['duty_id']]);
            foreach($role as $vo) {
                if($_POST[$vo['name']] == 1) {
                    $data['role_id'] = $vo['id'];
                    if(!db_find('oa_role_duty', $data))
                        M('oa_role_duty')->add($data);
                }
            }
            $newDutyRoles = db_select('oa_role_duty', ['duty_id'=>$data['duty_id']]);
            $this->updateUserRoleAfterDutyRoleChange($oldDutyRoles, $newDutyRoles);
            $this->success('添加成功！', U ( 'lists'), 600);
        } else {
            $duty = db_select('oa_duty', null, 'id desc');
            $role = db_select('oa_role');
            $this->assign('duty', $duty);
            $this->assign('role', $role);
            $this->display('add');
        }
    }

    function del()
    {
        $map['duty_id'] = $_GET['id'];
        $oldDutyRoles = db_select('oa_role_duty', $map);
        if(M('oa_role_duty')->where($map)->delete()) {
            $this->updateUserRoleAfterDutyRoleChange($oldDutyRoles, null);
            $this->success ( '删除' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        }
    }

    function edit()
    {
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['duty_id'] = $_POST['duty_id'];

            $roles = db_select('oa_role');
            $oldDutyRoles = db_select('oa_role_duty', ['duty_id'=>$data['duty_id']]);
            M('oa_role_duty')->where(['duty_id'=>$data['duty_id']])->delete();
            foreach ($roles as $r) {
                if($_POST[$r['name']] == 1) {
                    $data['role_id'] = $r['id'];
                    M('oa_role_duty')->add($data);
                }
            }
            $newDutyRoles = db_select('oa_role_duty', ['duty_id'=>$data['duty_id']]);
            $this->updateUserRoleAfterDutyRoleChange($oldDutyRoles, $newDutyRoles);
            $this->success ( '修改' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        }
        else {
            $duty = db_find('oa_duty', ['id'=>$_GET['id']]);
            $roles = db_select('oa_role');
            for ($i = 0; $i < count($roles); $i++) {
                if(db_find('oa_role_duty', ['duty_id'=>$duty['id'], 'role_id'=>$roles[$i]['id']])) {
                    $roles[$i]['checked'] = 'checked';
                }
            }
            $this->assign('duty', $duty);
            $this->assign('roles', $roles);
            $this->display();
        }
    }

    private function updateUserRoleAfterDutyRoleChange($oldDutyRoles, $newDutyRoles) {
        $duty_id = empty($oldDutyRoles) ? $newDutyRoles[0]['duty_id'] : $oldDutyRoles[0]['duty_id'];
        $oldRoles = array_column($oldDutyRoles, 'role_id');
        $newRoles = array_column($newDutyRoles, 'role_id');
        $dutyEmployees = db_select('oa_employee', ['duty_id'=>$duty_id]);
        foreach ($dutyEmployees as $emp) {
            if(!empty($oldRoles)) {
                $map['emp_id'] = $emp['id'];
                $map['role_id'] = array('in', $oldRoles);
                $map['assigned_by'] = 0;
                M('oa_user_role')->where($map)->delete();
            }
            if(!empty($newRoles)) {
                foreach ($newRoles as $r) {
                    $data['appid'] = $this->appId;
                    $data['emp_id'] = $emp['id'];
                    $data['role_id'] = $r;
                    $data['assigned_by'] = 0;
                    M('oa_user_role')->add($data);
                }
            }
        }
    }
}